﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using SMF.HowTo.Shared.Eventing;
using SMF.HowTo.UI.ViewModels.Events;

namespace SMF.HowTo.UI.ViewModels
{
    public class EasterEggViewModel : ViewModelBase
    {
        private readonly DispatcherTimer _hideTimer;
        private bool _isVisible = false;

        public bool IsVisible
        {
            get { return _isVisible; }
            set
            {
                if (_isVisible != value)
                {
                    _isVisible = value;
                    OnPropertyChanged("IsVisible");
                }
            }
        }

        public EasterEggViewModel()
        {
            _hideTimer = new DispatcherTimer();
            _hideTimer.Tick += (s, e) => Hide();
            EventAggregator.EventPublished += EventPublished;
        }

        private void EventPublished(Event evt)
        {
            if (evt is ShowEasterEggEvent)
            {
                TimeSpan timeSpan = ((ShowEasterEggEvent) evt).Argument;
                _hideTimer.Interval = timeSpan;
                this.IsVisible = true;
                _hideTimer.Start();
            }
        }

        private void Hide()
        {
            _hideTimer.Stop();
            this.IsVisible = false;
            var evt = new EasterEggHiddenEvent();
            EventAggregator.PublishEvent(evt);
        }
    }
}
